#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  int fd;
  void *addr;
  struct stat sb;

  if (argc != 2 || strcmp(argv[1], "--help") == 0)
    printf("usage: %s shm-name %s\n", argv[0], argv[1]);
  fd = shm_open(argv[1], O_RDONLY, 0);
  if (fd == -1)
    printf("Failed to open shared memory %s\n", argv[1]);
  if (fstat(fd, &sb) == -1)
    printf("Failed to stat fd\n");
  addr = mmap(NULL, sb.st_size, PROT_READ,MAP_SHARED,fd,0);
  if (addr == MAP_FAILED)
    printf("Failed to mmap\n");
  if (close(fd) == -1)
    printf("Failed to close fd\n");
  write(STDOUT_FILENO, addr, sb.st_size);
  printf("\n");
  return 0;
}
